package com.li.test.model7;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 - 消息队列的配置类
 */
@Configuration
public class  RabbitConfigModel7 {
    /**
     * 创建普通队列、普通交换机、队列绑定到交换机
     */
    @Bean
    public Queue normalQueue() {
        return QueueBuilder.durable("normal.queue")  //普通队列名字
                .deadLetterExchange("dlx.exchange")    //死信交换机
                .deadLetterRoutingKey("dlx.routing.key")//死信路由键
//                .ttl(5000)
                .maxLength(2)
                .build();
    }

    //正常交换机
    @Bean
    public DirectExchange normalExchange() {
        return new DirectExchange("normal.exchange");
    }

    //正常队列绑定到正常交换机，使用路由键city.gz
    @Bean
    public Binding normalQueueToDirectExchange(Queue normalQueue, DirectExchange normalExchange) {
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("city.gz");
    }

    /**
     * 创建死信队列、死信交换机、死信队列绑定到交换机
     */
    @Bean
    public Queue dlxQueue() {
        return QueueBuilder.durable("dlx.queue").build();
    }

    //死信交换机
    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange("dlx.exchange");
    }

    //死信队列绑定到死信交换机，使用路由键dlx.routing.key
    @Bean
    public Binding dlxQueueToDirectExchange(Queue dlxQueue, DirectExchange dlxExchange) {
        return BindingBuilder.bind(dlxQueue).to(dlxExchange).with("dlx.routing.key");
    }




    /**
     * 消息序列化成JSON字符串
     */
//    @Bean
//    public MessageConverter messageConverter() {
//        return new Jackson2JsonMessageConverter();
//    }
}